package com.company.cloud.uba.modules.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.company.cloud.uba.modules.app.models.UserIdMappingUpdate;
import com.company.cloud.uba.modules.app.service.UserIdMappingService;
import com.company.cloud.uba.modules.app.service.UserRegisterLogAppService;
import com.company.cloud.uba.modules.base.entity.UserRegisterLog;
import com.company.cloud.uba.modules.base.service.UserRegisterLogService;
import com.company.cloud.uba.utils.UbaIdUtils;
import com.company.common.server.config.mq.models.MQUserRegisterModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Service
public class UserRegisterLogAppServiceImpl implements UserRegisterLogAppService {

    @Autowired
    private UserRegisterLogService userRegisterLogService;
    @Autowired
    private UserIdMappingService userIdMappingService;

    @Override
    public void addUserRegisterLog(MQUserRegisterModel params) {
        UserRegisterLog entity=new UserRegisterLog();
        entity.setId(UbaIdUtils.getUserRegisterLogId());
        entity.setCompanyNo(params.getCompanyNo());
        entity.setUserId(params.getUserId());
        entity.setThirdUserId(params.getThirdUserId());
        entity.setThirdOpenId(params.getThirdOpenId());
        entity.setUserName(params.getUserName());
        entity.setToken(params.getToken());
        entity.setAnonyToken(params.getAnonyToken());
        entity.setRegisterType(params.getRegisterType());
        entity.setRegisterIp(params.getIp());
        entity.setClientType(params.getClientType());
        entity.setMobileNo(params.getMobileNo());
        entity.setLlType(params.getLlType());
        entity.setLongitude(params.getLongitude());
        entity.setLatitude(params.getLatitude());
        entity.setRegisterDate(params.getOpDate());
        entity.setSourceUrl(params.getSourceUrl());
        userRegisterLogService.save(entity);

        if(StrUtil.isNotEmpty(params.getAnonyToken())){
            UserIdMappingUpdate uimParams=new UserIdMappingUpdate();
            uimParams.setAnonyToken(params.getAnonyToken());
            uimParams.setLoginToken(params.getToken());
            uimParams.setUserId(params.getUserId());
            uimParams.setCompanyNo(params.getCompanyNo());
            uimParams.setClientType(params.getClientType());
            uimParams.setMobileNo(params.getMobileNo());
            uimParams.setOpDate(params.getOpDate());
            uimParams.setOpIp(params.getIp());
            userIdMappingService.updateUserIdMapping(uimParams);
        }
    }
}
